package com.example.springboottools.utils.kuaidi100;

import com.example.springboottools.entiy.ExcelExportUtil;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class KuaiDi100Utils {
    public static void main(String[] args) {
//        String filePath = "C:\\Users\\SH102909\\Downloads\\2024-10\\trbgaAWO2660.2024-10.log";
//        String excelPathName = "D:\\staples\\日常记录\\工作记录\\2024\\11\\11\\快递100\\"+System.currentTimeMillis() + "10月.xlsx";
//        exportKuaiDi100LogExcel(filePath,excelPathName);
        String filePath8 = "C:\\Users\\SH102909\\Downloads\\2024-08\\trbgaAWO2660.2024-08.log";
        String filePath9 = "C:\\Users\\SH102909\\Downloads\\2024-09\\trbgaAWO2660.2024-09.log";
        String filePath10 = "C:\\Users\\SH102909\\Downloads\\2024-10\\trbgaAWO2660.2024-10.log";
        BufferedReader reader = null;
        try {
             reader = new BufferedReader(new FileReader(filePath8));
            List<Map> maps8 = getMaps(reader);
            reader = new BufferedReader(new FileReader(filePath9));
            List<Map> maps9 = getMaps(reader);
            reader = new BufferedReader(new FileReader(filePath10));
            List<Map> maps10 = getMaps(reader);
           //maps8 按照code分组
            Map<Object, List<Map>> codeMap8 = maps8.stream().collect(Collectors.groupingBy(item -> item.get("code")));
            Map<Object, List<Map>> codeMap9 = maps9.stream().collect(Collectors.groupingBy(item -> item.get("code")));
            Map<Object, List<Map>> codeMap10 = maps10.stream().collect(Collectors.groupingBy(item -> item.get("code")));
            List<Map> maps8And9DuplicateList = new ArrayList<>();
            List<Map> maps9And10DuplicateList = new ArrayList<>();
            maps8.forEach(item->{
                String code = (String) item.get("code");
               if (codeMap9.containsKey(code)){
                   maps8And9DuplicateList.add(item);
               }
            });
            maps9.forEach(item->{
                String code = (String) item.get("code");
               if (codeMap10.containsKey(code)){
                   maps9And10DuplicateList.add(item);
               }
            });
            String excelPathName9 = "D:\\staples\\日常记录\\工作记录\\2024\\11\\11\\快递100\\"+System.currentTimeMillis() + "9月重复.xlsx";
            excelExport(excelPathName9, maps8And9DuplicateList);
            String excelPathName10 = "D:\\staples\\日常记录\\工作记录\\2024\\11\\11\\快递100\\"+System.currentTimeMillis() + "10月重复.xlsx";
            excelExport(excelPathName10, maps9And10DuplicateList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void exportKuaiDi100LogExcel(String filePath,String excelPathName) {
        // 用于读取文件
        BufferedReader reader = null;

        try {
            // 创建BufferedReader对象
            reader = new BufferedReader(new FileReader(filePath));
            // 读取文件内容

            String line;
            List<Map> mapListAll = getMaps(reader);
            excelExport(excelPathName, mapListAll);
        } catch (IOException e) {
            // 处理可能出现的IO异常
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void excelExport(String excelPathName, List<Map> mapListAll) throws IOException {
        ExcelExportUtil excelExportUtil = new ExcelExportUtil();
        String title = "title";
        //各个列的表头
        String[] heardList = {"时间","密钥","快递公司","快递公司编码","类型","IP"};
        //各个列的元素key值
        String[] heardKey = {"date", "key", "company","code","type","ip"};
        //需要填充的数据信息
        excelExportUtil.setTitle(title);
        excelExportUtil.setHeardList(heardList);
        excelExportUtil.setHeardKey(heardKey);
        excelExportUtil.setData(mapListAll);
        //  String s = System.currentTimeMillis() + "8月";
        // excelExportUtil.exportExportLocalSXSS("D:\\staples\\日常记录\\工作记录\\2024\\11\\11\\快递100\\" + s + ".xlsx");
        excelExportUtil.exportExportLocalSXSS(excelPathName);
    }

    private static List<Map> getMaps(BufferedReader reader) throws IOException {
        String line;
        List<Map> mapListAll = new ArrayList<>();
        while ((line = reader.readLine()) != null) {
            // 打印每一行
            System.out.println(line);
            String[] split = line.split("\\|");
            Map<String,Object> map1 = new HashMap<>();
            map1.put("date",split[0]);
            map1.put("key",split[1]);
            map1.put("company",split[2]);
            map1.put("code",split[3]);
            map1.put("type",split[4]);
            map1.put("ip",split[5]);
            mapListAll.add(map1);
        }
        return mapListAll;
    }
}
